<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Bindbank;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Bindbanks extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        isset($data['status']) ? $data['status'] : $data['status'] = 1;
        $data['listrow'] = 6;
        $gift = new Bindbank();
        $res = $gift->lists($data);
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        if ($request->isPost()){
            $data = $request->param();
            $validate = validate('bindbanks');
            $res = $validate->scene('addbank')->check($data);
            if (!$res){
                return apistatus(0,$validate->getError());
            }
            $insert['uid'] = $data['id'];
            $insert['username'] = $data['username'];
            $insert['idcard'] = $data['idcard'];
            $insert['bankcard'] = $data['bankcard'];
            $insert['bankphone'] = $data['bankphone'];
            $insert['bankname'] = $data['bankname'];
            $insert['create_time'] = time();
            $code = $data['code'];
            $oldcode = \db('home_code')
                ->field(['id','code','phonenum','expire_time'])
                ->where('phonenum',$data['bankphone'])
                ->where('status',1)
                ->order('create_time desc')
                ->find();
            if (empty($oldcode)){
                return apistatus(0,'验证码过期请重新获取验证码');
            }
            if ($oldcode['phonenum'] !== $data['bankphone']){
                return apistatus(0,'请输入获取验证码的手机号');
            }
            if ( $oldcode['code'] == $code && $oldcode['expire_time']>time()){
                $bankid = \db('home_bindbank')->insertGetId($insert);
                if ($bankid){
                    //如果新建成功就删除该手机号对应的验证码缓存
                    Db::table('rd_home_code')->where('id',$oldcode['id'])->update(['status'=>2]);
                    $da['id'] = $bankid;
                    $da['name'] = $data['bankname'];
                    return apistatus(1,'银行卡绑定成功',$da);
                }else{
                    return apistatus(0,'绑定失败请重试');
                }
            }else{
                return apistatus(0,'验证码过期或错误');
            }
        }else{
            return apistatus(0,'非法请求');
        }


    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $data = $request->param();
        if (empty($data['id'])){
            return apistatus('0','请提交银行卡id');
        }
        $res = \db('home_bindbank')->where('id',$data['id'])->update(['status'=>2,'delete_time'=>time()]);
        if ($res){
            return apistatus(1,'删除成功');
        }else{
            return apistatus(0,'删除失败请重试');
        }
    }
    /**
     * 查看我的绑定银行

     */
    public  function mybankList(Request $request)
    {
        $data = $request->param();
        $validate = validate('bindbanks');
        $res = $validate->scene('getbanklist')->check($data);
        if (!$res){
            return apistatus(0,$validate->getError());
        }
        $where['uid'] = $data['id'];
        $where['status'] = 1;
        $banklist = model('bindbank')->where($where)->order('create_time desc')->paginate();
        if (!empty($banklist)){
            return apistatus(1,'列表获取成功',$banklist);
        }else{
            return apistatus(0,'无数据');
        }
    }





}
